<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <home></home>
  </div>


  <script src="./vue.js"></script>
  <script>
    // 定义一个第三方实例
    const bus = new Vue();
    const Child = {
      template: `
        <div>
          <h3>我是子组件1</h3>
          <button @click="biubiubiu">向父组件发送数据</button>
        </div>
      `,
      data(){
        return {
          msg: '我是子组件1数据'
        }
      },
      methods: {
        biubiubiu(){ 
          bus.$emit('piupiupiu', this.msg)
        }
      }
    }
    const Child2 = {
      template: `
        <div>
          <h3>我是子组件2</h3>
          {{ msg }}
        </div>
      `,
      mounted(){
        // 这个方法会自动触发 （在组件初始化之后）
        /* bus.$on('piupiupiu', (msg)=>{
          this.msg = msg;
        }) */
        // const _this = this;
        bus.$on('piupiupiu', function(msg){
          
          this.msg = msg;
        }.bind(this))
      },
      data(){
        return {
          msg: ''
        }
      },
      
    }
    const Home = {
      template: `
        <div>
          <h2>我是父组件1</h2>
        
          <hr/>
          <child></child>
          <hr/>
          <child2></child2>
        </div>
      `,
      data(){
        return {
          msg: ''
        }
      },
      
      components: {
        Child,
        Child2
      }
    }
    Vue.component('Home', Home);
  
    const vm = new Vue({
      el: '#app',
      data: {

      }
    })
    
  </script>
</body>

</html>